home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / MODULA_2 / 2394.ZIP / M2TOOLS1.ZIP / DECIMALO.DEF < prev    next >
Text File  |  1990-08-02  |  3KB  |  73 lines

  1. DEFINITION MODULE DecimalOps;
  2.  
  3.  
  4.   PROCEDURE PrepareDecimal (VAR Str1 : ARRAY OF CHAR;
  5.                                 Len,
  6.                                 DP   : CARDINAL);
  7.  
  8.     (* This procedure is normally used by the decimal conversion
  9.        procedures, but is provided here as an extra tool.
  10.  
  11.        The procedure's main aim is to make sure that a decimal is in the
  12.        correct format (i.e. that it has the correct number of decimal
  13.        places and no unnecessary characters)
  14.  
  15.        It does the following functions (read as "number becomes new
  16.        number") :
  17.  
  18.          i) Front Zero added if number < 1 :
  19.               '.125'     -->   '0.125'
  20.  
  21.         ii) Front zero removed if next character is not a decimal point :
  22.               '07.125'   -->   '7.125'
  23.  
  24.        [Note - For steps i) and ii) above, if the number is negative the
  25.                resultant string will be the same format as above but with
  26.                the `-' sign in front.  e.g.  -.125   -->   -0.125]
  27.  
  28.        iii) If too few decimal places then zeros added to make correct
  29.             length :
  30.               '1.25' (with 3 d.p.)   -->   '1.250'
  31.  
  32.         iv) If there are too many decimal places, the remove least
  33.             significant figures :
  34.               '1.125' (with 2 d.p.)  -->   '1.12'
  35.  
  36.          v) Make sure string is correct length (right justified) :
  37.               '1.125' (with 3 d.p. & len 10)   -->   '     1.125'
  38.  
  39.         vi) If input string is null (i.e. Str [0] = 0C) :
  40.               '' (with 3 d.p. & len 10)   -->   '     0.000' 
  41.  
  42.        vii) If input string is an integer, then insert decimal places :
  43.               '3' (with 3 d.p. & length 10)   -->   '     3.000'  *)
  44.  
  45.  
  46.   PROCEDURE DecToString (    Dec           : ARRAY OF CHAR;
  47.                          VAR Str           : ARRAY OF CHAR;
  48.                              Len,
  49.                              DecimalPlaces : CARDINAL);
  50.  
  51.     (* This procedure will convert a decimal Dec to a string Str.  Any 
  52.        spaces at the front of the string will be removed.
  53.        [DTS = DecToString]
  54.  
  55.        e.g. DTS ('  1.125', Str, 7, 3)   -->   Str = '1125'
  56.             DTS ('  1.125', Str, 7, 2)   -->   Str = '112'
  57.             DTS ('  1.125', Str, 3, 3)   -->   Str = '1125'  *)
  58.  
  59.  
  60.   PROCEDURE StringToDec (    Str           : ARRAY OF CHAR;
  61.                          VAR Dec           : ARRAY OF CHAR;
  62.                              Len,
  63.                              DecimalPlaces : CARDINAL);
  64.  
  65.     (* This procedure will convert a string of numbers into a decimal string
  66.        of numbers.  No attempt is made to check that Str is a valid number.
  67.        [STD = StringToDec]
  68.  
  69.        e.g. STD ('1125', Dec, 5, 3)   -->   Dec = '1.125'
  70.             STD ('1125', Dec, 10, 2)  -->   Dec = '     11.25'
  71.             STD ('1125', Dec, 4, 3)   -->   Dec = '1.125'  *)
  72.  
  73. END DecimalOps.